#! /usr/bin/python
# -*- coding:utf-8 -*-

## ========================================================================
## Copyright (C) 2007-2013  Jean-Philippe GOLAY
##
## This program is free software; you can redistribute it and/or modify
## it under the terms of the GNU General Public License as published by
## the Free Software Foundation; either version 2 of the License, or
## (at your option) any later version.
##
## This program is distributed in the hope that it will be useful,
## but WITHOUT ANY WARRANTY; without even the implied warranty of
## MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
## GNU General Public License for more details.
##
## You should have received a copy of the GNU General Public License
## along with this program; if not, write to the Free Software
## Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
## ========================================================================

import ConfigParser
import logging
from variable import *

class CFG:

  def __init__(self):
    self.config = ConfigParser.RawConfigParser()
    logging.basicConfig(filename=Glob.LOG_FILENAME,level=logging.DEBUG)
    self.section1=u"Initialisation"


  def defaultFile(self):
    self.config.add_section(self.section1)
    self.config.set(self.section1, u'config', '1')
    self.config.set(self.section1, u'admin', '1')
    self.openFile()
    try:
      self.config.write(self.configfile)
    except error:
      logging.debug(u'CFG: defaultFile() error : %s' % error )
    self.closeFile()


  def readValue(self, key):
    self.config.read(Glob.INI_FILENAME)
    return self.config.get(self.section1, key)


  def writeValue(self, key, value):
    self.config.read(Glob.INI_FILENAME)
    self.config.set(self.section1, key, value)
    self.openFile()
    try:
      self.config.write(self.configfile)
    except error:
      logging.debug(u'CFG: writeValue() error : %s' % error )
    self.closeFile()


  def deleteValue(self, key):
    self.config.read(Glob.INI_FILENAME)
    return self.config.remove_option(self.section1, key)


  def openFile(self):
    try:
      self.configfile=open(Glob.INI_FILENAME, u'wb')
    except error:
      logging.debug(u'CFG: open() error : %s' % error )


  def closeFile(self):
    try:
      self.configfile.close()
    except error:
      logging.debug(u'CFG: close() error : %s' % error )
